package com.moli.iov.alarm.remote.impl;

import com.moli.iov.alarm.model.dto.AlarmVehicleDto;
import com.moli.iov.alarm.remote.IAlarmVehicleStatisticsService;
import com.moli.iov.alarm.service.stat.AlarmMangerDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author fudh
 * @ClassNmme AlarmVehicleStatisticsServiceImpl
 * @date 2019/6/3 16:20
 * @Description: TODO
 */
@Slf4j
@Service(group = "${service.group}")
public class AlarmVehicleStatisticsServiceImpl implements IAlarmVehicleStatisticsService {


    @Autowired
    private AlarmMangerDao alarmMangerDao;

    @Override
    public List<AlarmVehicleDto> queryVehicleAlarmAnalyze(Date startTime, Date endTime, String vin) {
        List<AlarmVehicleDto> alarmVehicleDtos = new ArrayList<>();
        if (startTime == null || endTime == null || vin == null) {
            log.error("startTime,endTime,vin参数有为空，startTime={}，endTime={}，vin={}", startTime, endTime, vin);
            return alarmVehicleDtos;
        }
        List<Document> documents = alarmMangerDao.queryVehicleAlarmAnalyze(startTime, endTime, vin);
        if (documents == null) {
            return alarmVehicleDtos;
        }

//        Map<String, AlarmVehicleDto> miniteCountMap = new LinkedHashMap<>();
        //查询分钟数
//        List<String> miniterList = Util.getMiniterList(startTime, endTime);
//        for (String s : miniterList) {
//            AlarmVehicleDto alarmVehicleDto = new AlarmVehicleDto();
//            alarmVehicleDto.setAlarmCount(0);
//            alarmVehicleDto.setMessageDate(s);
//            miniteCountMap.put(s, alarmVehicleDto);
//        }
        for (Document document : documents) {
            String mtime = (String) document.get("_id");
            Integer total = (Integer) document.get("total");
            AlarmVehicleDto alarmVehicleDto = new AlarmVehicleDto();
            alarmVehicleDto.setMessageDate(mtime);
            alarmVehicleDto.setAlarmCount(total);
            alarmVehicleDtos.add(alarmVehicleDto);
        }
        return alarmVehicleDtos;
    }
}
